package com.yanzhu.api;

import com.yanzhu.bean.ResponseBean;
import com.yanzhu.service.RankService;
import com.yanzhu.service.UserService;
import com.yanzhu.service.VideoService;
import com.yanzhu.service.model.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: 方炎柱
 * @Description:
 * @Date: Created in 15:22 2018/2/5
 * @Modified:
 */
@AllArgsConstructor
@RestController
public class RankAPI {
    private final VideoService videoService;

    private final RankService rankService;

    private final UserService userService;

    //添加排行
    @RequestMapping(value="/insertRank")
    public ResponseBean insertRank(HttpServletRequest request) throws Exception {
        String uid = request.getParameter("uid");
        if(uid == null || uid.equals(""))
            return ResponseBean.fail("缺少用户ID参数!");

        String vid = request.getParameter("vid");
        if(vid == null || vid.equals(""))
            return ResponseBean.fail("缺少视频ID参数!");

        String like_speed = request.getParameter("like_speed");
        if(like_speed == null || like_speed.equals(""))
            return ResponseBean.fail("缺少速度参数!");

        Video video = videoService.selectById(Long.parseLong(vid));
        if(video == null)
            return ResponseBean.fail("该视频不存在!");

        User user = userService.queryUserInfo(Long.parseLong(uid));
        if(user == null)
            return ResponseBean.fail("该改用户不存在!");

        Rank rank = new Rank();
        rank.setUid(Long.parseLong(uid));
        rank.setVid(Long.parseLong(vid));
        rank.setLike_speed(Integer.parseInt(like_speed));

        if(rankService.insert(rank) < 1)
            return ResponseBean.fail("数据库错误！");

        return ResponseBean.ok(rank);
    }

    //查询排行
    @RequestMapping(value="/queryVideoLikeRank")
    public ResponseBean queryVideoLikeRank(HttpServletRequest request) throws Exception {

        String vid = request.getParameter("vid");
        if(vid == null || vid.equals(""))
            return ResponseBean.fail("缺少视频ID参数!");

        Video video = videoService.selectById(Long.parseLong(vid));
        if(video == null)
            return ResponseBean.fail("该视频不存在!");

        return ResponseBean.ok(rankService.queryVideoLikeRank(Long.parseLong(vid)));
    }
}
